package com.token.iot.bratterycharging.gateway.business;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.gateway.IDGeneration;
import com.token.iot.bratterycharging.gateway.api.Constant;
import com.token.iot.bratterycharging.gateway.utils.ByteUtil;

/**
 * 设备参数设置
 * 
 * @author chenzhixuan
 *
 */
@Component("DCCDuplexProcessor")
public class DCCDuplexProcessor extends DuplexProcessor {

	@Override
	public String request(String imei, Map<String, String> params) throws PendingException {
		String c = "DCC";
		String sessionId = IDGeneration.createSeesionID();
		String command = null;

		// 端口号
		String money = params.get(Constant.MONEY);
		money = StringUtils.leftPad(money, 3,"0");

		String first = params.get(Constant.FIRST);
		first =StringUtils.leftPad(first, 3,"0");
		String second = params.get(Constant.SECOND);
		second = StringUtils.leftPad(second, 3,"0");
		String third = params.get(Constant.THIRD);
		third = StringUtils.leftPad(third, 3,"0");

		// 300W，一次刷卡?元，第一次?分钟，第二次?分中，第三次?分钟
		command = c + sessionId + "/" + "300"+money+first+second+third;
		loger.info("the send =" + new String(command));
		byte[] cc = ByteUtil.addFlag(command.getBytes());
		context.writeAndFlush(cc);
		return sessionId;

	}

	@Override
	public Boolean response(String response) throws PendingException {
		String val = response.substring(14);
		if ("1".equals(val)) {
			return true;
		} else {
			return false;
		}
	}
	
	
	public static void main(String[] args) {
		
		System.out.println(StringUtils.leftPad("aaa", 3,"0"));
	}

}
